Ext.define('aps.revivirItemPanel', {
    extend: 'Ext.form.Panel',
    alias : 'aps.revivirItemPanel',

    title :   'Revivir Item',
    id:       'panel_revivir_item',
    name:     'panel_revivir_item',
    layout:   'fit',
    frame:    true,
    closable: true,
    border:   false,
  
    initComponent: function() {     
		var grid_items_revivir = Ext.create('aps.itemGridRevivir',{
		    margin:         '20 0 0 0',
        });
        this.items = [{
            xtype:      'form',
            title:      'Revivir Item',
            autoScroll: true,
            fieldDefaults: {
                blankText:     'Este campo no puede ser nulo',
                msgTarget:     'side',
                autoFitErrors: false
            },
            items: [
                {
                    xtype:     'label',
                    margin:    '50 20 20 20',
                    style:     'font-weight:bold;',
                    text:      'Seleccione el item que desea revivir'
                },
                    grid_items_revivir,
            ]
        }];
        this.buttons =[{
            text:     'Aceptar',
            iconCls:  'save-icon',
            grid:     grid_items_revivir,
            handler:  this.form_submit_handler
        }, {
            text:    'Cancelar',
            iconCls: 'cancel-icon',            
            handler: this.form_cancel_handler
        }];
        
        this.listeners = {
            close : function(){
                // actualizamos el store
                var store = Ext.data.StoreManager.lookup('itemStore');
                store.load({
                    params : {
                        idfase : Ext.getCmp('combo-fase').valueModels[0].data.idfase,
                    }
                });
                // volvemos al panel de administracion
                Ext.getCmp('area-central').agregar_pestanha('panel_administrar_item', null);
            }
        };
        
        this.callParent(arguments);
    },
    
    form_submit_handler: function(){
        var records = this.grid.getSelectionModel().getSelection();
        if(records == null || records.length == 0){
            Ext.Msg.alert('ERROR','Debe seleccionar un item para revertir');
        }
        else{
            var id_item = records[0].data.id;
            var self = this;
            var form = self.up('form').getForm();
            if(form.isValid()){
                form.submit({
                    method:    'POST',
                    url:       '/revivir_item',
                    params: {id_item: id_item},
                    waitTitle: 'Connecting', 
                    waitMsg:   'Sending data...',
                    success: function(form,action){
                        Ext.Msg.alert('INFO','Item revivido con exito!', function(btn, text){
                            if (btn == 'ok'){
                                self.up('panel').close();
                            }
                        });
                    },
                    failure: function(form, action){
                        Ext.Msg.alert('ERROR','Ocurrio un problema al revivir el item!');
                    }
                });
            }
         }   
    },
    
    form_cancel_handler: function() {
        this.up('panel').close();
    }
});





